from poplib import *
import urllib

#Checks mailserver for messages and scrapes them for presentation links
def getwdlinksfromemail(wdclientname, mailserver, username, password):
	popserver = POP3(mailserver)
	popserver.user(username)
	popserver.pass_(password)
	inbox = popserver.list()
	if len(inbox[1]) == 0:
		popserver.quit()
	else:
		wiredrivelinks = []
		messagelist = popserver.list()[1]
		for message in range(1, len(inbox[1])+1):
			messagesource = popserver.retr(message)[1]
			messagebody = str(messagesource)
			foundurl = messagebody.find('http://' + wdclientname + '.wiredrive.com/r/p/?presentation=')
			# If the above 'find' gets a hit, it always has a 32-character hash after it to end the URL
			fullurl = messagebody[foundurl:foundurl+75]
			if len(fullurl) == 75:
				wiredrivelinks.append(messagebody[foundurl:foundurl+75])
		popserver.quit()
		return(wiredrivelinks)
		
def getwdlinksfromemailSSL(wdclientname, mailserver, username, password):
	popserver = POP3_SSL(mailserver)
	popserver.user(username)
	popserver.pass_(password)
	inbox = popserver.list()
	if len(inbox[1]) == 0:
		popserver.quit()
	else:
		wiredrivelinks = []
		messagelist = popserver.list()[1]
		for message in range(1, len(inbox[1])+1):
			messagesource = popserver.retr(message)[1]
			messagebody = str(messagesource)
			foundurl = messagebody.find('http://' + wdclientname + '.wiredrive.com/r/p/?presentation=')
			# If the above 'find' gets a hit, it always has a 32-character hash after it to end the URL
			fullurl = messagebody[foundurl:foundurl+75]
			if len(fullurl) == 75:
				wiredrivelinks.append(messagebody[foundurl:foundurl+75])
		popserver.quit()
		return(wiredrivelinks)

#Takes a link from a WireDrive e-mail and get's the RSS feed for that output
def findwdrssurl(email_link):
	presentationpage = urllib.urlopen(email_link)
	presentationsource = presentationpage.read().decode("utf8")
	feedurlstart = presentationsource.find('http://www.wdcdn.net/rss')
	feedurlend = presentationsource.find('<!-- GLOBAL CSS -->') - 18
	return(presentationsource[feedurlstart:feedurlend])

#Takes a raw RSS feed and gets links to the media files
def getwdmediafiles(feedcontent):
	enclosureurls = []
	enclosuresfound = 0
	totalenclosures = feedcontent.count('enclosure')
	while enclosuresfound < totalenclosures:
		foundenclosurestart = feedcontent.find('enclosure') + 15
		foundenclosureend = feedcontent.find('length') - 19
		enclosureurls.append(str(feedcontent[foundenclosurestart:foundenclosureend]))
		#removing portion of feed after discovered link
		feedcontent = feedcontent[foundenclosureend+24:]
		enclosuresfound = enclosuresfound + 1
	return(enclosureurls)

#Takes a raw RSS feed and gets the titles. First item is the output name; remaining are video titles
def getwdtitles(feedcontent):
	videotitles = []
	titlesfound = 0
	totaltitles = feedcontent.count('<title><![CDATA[')
	while titlesfound < totaltitles:
		foundtitlestart = feedcontent.find('<title><![CDATA') + 16
		foundtitleend = feedcontent.find(']]></title>')
		foundtitle = feedcontent[foundtitlestart:foundtitleend]
		videotitles.append(foundtitle)
		#removing portion of feed after discovered link
		feedcontent = feedcontent[foundtitleend+11:]
		titlesfound = titlesfound + 1
	return(videotitles)

#Takes a raw RSS feed and gets the date the e-mail was sent
def getwdfeeddate(feedcontent):
	datestart = feedcontent.find('<pubDate>') + 9
	dateend = datestart + 16
	return(feedcontent[datestart:dateend])